<?php
/**
 * 是否购买了
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Models\ProductModel;
use App\Http\Models\ProjectModel;
use App\Http\Models\VipModel;
use App\Http\Models\VipSettingsModel;
use App\Http\Models\MemberExchangeOrderModel;
use App\Http\Models\MemberExchangeOrderDetailModel;
use App\Http\Models\MemberPointRecordModel;
use App\Http\Models\CacheModel;

class PaymentController extends HomeController
{
    private $_productKeys = array(
            '3' => 'editor_promotion_link_isedit',
            '4' => 'editor_loading_desc'
        );
    private $_productDesc = array(
            '3' => '单次去版权信息功能',
            '4' => '单次去加载页官方宣传信息功能'
        );
    
    /*检查相关功能是否已付费*/
    public function info(Request $request)
    {
        try{
            $this->validate($request, [
                'apptoken' => 'required',
                'productid' => 'required'
            ]);
            $apptoken = $request->input('apptoken');
            $productid = $request->input('productid');
            $projectInfo = ProjectModel::info($apptoken);

            $item_key = VipSettingsModel::getProductItemKey($this->user->id, $projectInfo->id, $productid);
            $endTime = VipSettingsModel::getItemValue($this->user->id, $item_key, 0);

            if($endTime > $this->time) {
                return response()->json(build_result(['code' => 1, "msg" => "已经购买"]));
            } else {
                return response()->json(build_result(['code' => 0, "msg" => "没有购买"]));
            }
        }catch(\Exception $e) {
            return response()->json(build_result('', '10001', $e->getMessage()));
        }
    }

    /**
     * 购买相关功能
     */
    public function buy(Request $request)
    {
        try{
            $this->validate($request, [
                'apptoken' => 'required',
                'productid' => 'required'
            ]);
            $apptoken = $request->input('apptoken');
            $productid = $request->input('productid');
            $projectInfo = ProjectModel::info($apptoken);
            if(!isset($this->_productKeys[$productid])) return response()->json(build_result('', '61001', $e->getMessage()));
            $needPoint = ProductModel::getUnitPriceBykey($this->_productKeys[$productid]);
            $allPoint = VipSettingsModel::getItemValue($this->user->id, 'allpoint', 0);
            if($needPoint > $allPoint) return response()->json(build_result('', '61002', $e->getMessage()));


            if($productid == 3) {
                CacheModel::del('activityrulebanner:' . $apptoken);
                CacheModel::del('resultPagebanner:' . $apptoken);
            } elseif($productid == 4) {
                $canEditLoadingDesc =  VipModel::getLevelExtraInfoByKey($this->user->id, 'editor_loading_desc');
                if(!$canEditLoadingDesc) return response()->json(build_result('', '61003', $e->getMessage()));
            }

            // 生成订单
            $order = MemberExchangeOrderModel::create([
                    "uid" => $this->user->id,
                    "nickname" => $this->user->username,
                    "sum" => $needPoint,
                    "paysum" => $needPoint,
                    "desc" => $this->_productDesc[$productid],
                    "create_time" => $this->time,
                    "modify_time" => 0,
                    "status" => 1
                ]);
            if($order->id) {
                //扣除赢销币
                VipSettingsModel::updateItemValue($this->user->id, 'allpoint', -$needPoint);
                //添加消耗记录
                $record = MemberPointRecordModel::create([
                    'uid' => $this->user->id,
                    'nickname'=> $this->user->username,
                    'to_uid' => $this->user->id,
                    'orderid' => $order->id,
                    'channel' => 3,
                    'point' => -$needPoint,
                    'price' => 0,
                    'desc'  => $this->_productDesc[$productid],
                    'create_time' => NOW_TIME
                ]);
                //添加购买详情
                $quality = 30; //订购天数
                $unit_point = 0;
                $discount = 0;
                MemberExchangeOrderDetailModel::create([
                    'orderid'   => $order->id,
                    'appid'     => $projectInfo->id,
                    'productid' => $productid,
                    'quality'   => $quality,
                    'unit_point'=> $unit_point,
                    'unit_name' => $unit_name,
                    'discount'  => $discount,
                    'real_point'=> -$needPoint,
                    'create_time' => $this->time,
                    'status' => 1
                ]);
                // 添加时间
                $item_key = VipSettingsModel::getProductItemKey($this->user->id, $projectInfo->id, $productid);
                $row = VipSettingsModel::updateItemValue($this->user->id, $item_key, $quality * 86400);
                return response()->json(build_result(['code' => 1, "msg" => "支付成功"]));
            } else {
                return response()->json(build_result(['code' => 0, "msg" => "支付失败"]));
            }

        }catch(\Exception $e) {
            return response()->json(build_result('', '10001', $e->getMessage()));
        }
    }
}
